<template>
  <div>
    <form-select title="发券商户" :required=true name="actType" :options="mchtNameOptions" :value.sync="bussion"></form-select>
    <form-select title="电子券" :required=true name="actType" :options="couponList" :value.sync="content.coupon" ></form-select>
    <form-date-time title="活动开始时间" :required=true name="actBegDate" :value.sync="begTime" :disabled="isRuleListLen" @is-change="isChange"></form-date-time>
    <form-date-time title="活动结束时间" :required=true name="actEndDate"  @is-change="isChange" :value.sync="endTime"></form-date-time>
  </div>
</template>

<style lang="less" scoped>
  
</style>

<script>
  import {
    dateTime
  } from "ui/collections/general.js";
  export default {
    data() {
      return {
        begTime: "",
        endTime: "",
        bussion: ""
      };
    },
    props: {
      content: {
        require: true,
        type: Object
      },
      mchtNameOptions: Array,
      couponList: Array,
    },
    watch: {
      bussion(newVal) {
        this.content.bussion = newVal;
        let _data = {};
        _data.mchtId = this.bussion.toString();
        _data.aoData = JSON.stringify([{
            name: "iDisplayStart",
            value: 1
          },
          {
            name: "iDisplayLength",
            value: 1000000
          }
        ]);
        _data = JSON.stringify(_data);
        let self = this;
        self
          .$post({
            url: "/api/v2" + projectName_Url + "/plat/coupon/query/" + self.bussion,
            data: _data,
            method: "POST"
          })
          .then(res => {
            if (res.content.total === 0) {
              self.mchtusrList = [];
              Midway.alert({
                title: "提示",
                content: "该商户暂无优惠券,请先添加"
              });
              this.couponList = [];
              return false;
            }
            self.couponList = res.content.couponList.map(ele => {
              return {
                key: ele.couponId + "-" + ele.name,
                value: ele.couponId
              };
            });
          });
      },
      begTime(newVal) {
        this.content.begTime = this.signTime(newVal);
      },
      endTime(newVal) {
        this.content.endTime = this.signTime(newVal);
      }
    },
    methods: {
      signTime(newVal) { //获取时间戳
        let temp = [];
        let f = newVal
          .split(" ")
          .map((ele, index) => {
            if (index === 0) return ele.split("-").map(t => parseInt(t));
            if (index === 1) return ele.split(":").map(t => parseInt(t));
            return temp;
          })
          .join(",")
          .split(",");
        return new Date(
          Number(f[0]),
          Number(f[1]),
          Number(f[2]),
          Number(f[3]),
          Number(f[4])
        ).getTime();
      },
      isChange () {
        if (this.begTime && this.endTime) {
         this.$emit('is-change',this.content) 
        }
      }
    },
    ready() {
      dateTime({
        format: "yyyy-mm-dd hh:ii",
        minView: "0"
      });
    }
  };
</script>


